<?php


class Order {
	private $sum = 0;
	private $id;
	//object
	private $shop;
	private $date;
	private $description="";
	private $status;
	private $info;
	//array of objects
	private $stocks = array();

	public function __construct(array $order=array("id" =>null,"date"=>null,"info"=>null,"shop"=>null,"status"=>null,"stocks"=>array())){
		$this->setId($order["id"]);
		$this->setDate($order["date"]);
		$this->setInfo($order["info"]);
		$this->setShop($order["shop"]);
		$this->setStatus($order["status"]);
		$this->setStocks($order["stocks"]);
		$this->stocksSum();
	}

	public function addStock( stock $stock){

		$this->addInSum($stock,$stock->getOrderQuantity());
		$flag=true;
		if ( count($this->stocks)==0){
			$this->stocks[]=$stock;
		} else {
			foreach($this->stocks as $obj){
				if (strcmp($obj->getName(),$stock->getName())==0){
					$obj->setOrderQuantity($obj->getOrderQuantity()+$stock->getOrderQuantity());
					$flag=false;
					break;
				}
			}
			if ($flag){
				$this->stocks[]=$stock;
			}
		}
	}
	public function clear(){
		$this->stocks = array();
		$this->sum = 0;

	}

	public function updateInSum(stock $stock){
		if ((int)$stock->getOrderQuantity() !=0 ){

			foreach($this->stocks as $obj){
				if (strcmp($obj->getName(),$stock->getName())==0 && ($stock->getOrderQuantity() != $obj->getOrderQuantity())){
					if ($stock->getOrderQuantity()> $obj->getOrderQuantity()){

						$this->addInSum($stock,$stock->getOrderQuantity()-$obj->getOrderQuantity());

					} else {

						$this->removeInSum($stock,$obj->getOrderQuantity()-$stock->getOrderQuantity());
					}
					$obj->setOrderQuantity($stock->getOrderQuantity());
					break;
				}
			}
		}

	}

	public function addInSum(stock $stock,$quantity){
		if ((int)$quantity !=0 ){

			if ($stock->getPromoPrice() !=null){
				$price = floatval($stock->getPromoPrice());
			} else {
				$price = floatval($stock->getPrice());
			}
			$this->sum += (int)$quantity*$price;
		}
	}

	public function removeInSum(stock $stock,$quantity){
		if ((int)$quantity !=0 ){

			if ($stock->getPromoPrice() !=null){
				$price = floatval($stock->getPromoPrice());
			} else {
				$price = floatval($stock->getPrice());
			}
			$this->sum -= (int)$quantity*$price;
		}
	}

	public function deleteStock( stock $stock){
		$this->removeInSum($stock,$stock->getOrderQuantity());
		$temp =array();
		foreach($this->stocks as $obj){
			if (strcmp($obj->getName(),$stock->getName())!=0){
				$temp[] = $obj;
			}
		}

		$this->stocks=$temp;
	}

	public function stocksNumber(){
		return count($this->stocks);
	}

	public function stocksSum(){
		foreach ($this->stocks as $stock){
			if ((int)$stock->getOrderQuantity() !=0 ){
				if ($stock->getPromoPrice() !=null){
					$price = floatval($stock->getPromoPrice());
				} else {
					$price = floatval($stock->getPrice());
				}
				$this->sum += (int)$stock->getOrderQuantity()*$price;
			}
		}

	}


	public function setDescription($description) {
		$this->description = $description;
	}

	public function getDescription() {
		return $this->description;
	}
	public function setSum($sum) {
		$this->sum = $sum;
	}


	public function getSum() {
		return $this->sum;
	}

	public function setDate($date) {
		$this->date = $date;
	}


	public function getDate() {
		return $this->date;
	}


	public function setId($id) {
		$this->id = $id;
	}


	public function getId() {
		return $this->id;
	}


	public function setInfo($info) {
		$this->info = $info;
	}


	public function getInfo() {
		return $this->info;
	}


	public function setShop($shop) {
		$this->shop = $shop;
	}


	public function getShop() {
		return $this->shop;
	}


	public function setStatus($status) {
		$this->status = $status;
	}


	public function getStatus() {
		return $this->status;
	}


	public function setStocks($stocks) {
		$this->stocks = $stocks;
	}


	public function getStocks() {
		return $this->stocks;
	}



}
?>